import Scanner from "./scanner";
import nestTokens from "./nestTokens";

export default function parseTemplateToTokens(templateStr) {
    const tokens = [];
    const scanner = new Scanner(templateStr);
    let word;

    while (scanner.eos()) {
        word = scanner.scanUtil('{{');
        scanner.scan('{{');
        if (word !== '') {
            tokens.push(['text', word]);
        }

        word = scanner.scanUtil('}}');
        scanner.scan('}}');
        if (word !== '') {
            switch (word[0]) {
                case '#':
                    tokens.push(['#', word.substring(1)]);
                    break;
                case '/':
                    tokens.push(['/', word.substring(1)]);
                    break;
                default:
                    tokens.push(['name', word]);
            }
        }
    }

    return nestTokens(tokens);
};